/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: ipc_client_requester.h
  *
  * Purpose: implementation client requester with ipc protocol for 'COM' Functional
  *
  * Developer:
  *   wen.gu , 2020-07-28
  *
  * TODO:
  *
  ***************************************************************************/

  /******************************************************************************
   **    INCLUDES
   ******************************************************************************/
#ifndef __PANDA_COM_RUNTIME_IPC_CLIENT_REQUESTER_H__
#define __PANDA_COM_RUNTIME_IPC_CLIENT_REQUESTER_H__

#include <cstdint>
#include <memory>
#include <string>
#include <functional>
#include <map>

#include "panda/core/function_executor.h"
#include "panda/com/com_types.h"
#include "panda/sys/ipc_client.h"
#include "panda/com/runtime/ipc_types.h"
#include "panda/com/runtime/ipc_packet.h"
#include "panda/com/runtime/message.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace com 
{
namespace runtime
{

/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/
class COM_CLASS IpcClientRequester final
{
public:
    using PandaErrc = panda::core::PandaErrc;
    using IpcClient = panda::sys::IpcClient;

public:
    IpcClientRequester();
    ~IpcClientRequester();

public:
    /** try to connect to service,
    *  if failed, then this function will be return immediately
    */
    PandaErrc connect(const std::string& srvAddr);

    /** disconnect from service */
    void disconnect();
public:
    PandaErrc request(const std::string& method, const uint8_t* param, uint32_t size, IpcPacketData& reply, int32_t waitMs = COM_REQ_WAIT_INFINITE);
    PandaErrc requestAsync(const std::string& method, const uint8_t* param, uint32_t size, IpcClientReplyHandler rpyHandler);

    PandaErrc subscribeSignal(const std::string& sig, IpcClientSignalHandler sigHandler);

    PandaErrc unsubscribeSignal(const std::string& sig);

private:
    class impl;
    std::unique_ptr<impl> mImpl;

};

} /** namespace runtime */
} /** namespace com */
} /** namespace panda */


#endif /** !__PANDA_COM_RUNTIME_IPC_CLIENT_REQUESTER_H__ */